﻿using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.Department
{
    /// <summary>
    /// ODepartment 的摘要说明。
    /// </summary>
    public partial class ODepartmentPage : Page
    {
        private static string DepartmentDescription;
        private static string DepartmentName; //部门名称
        private static int DeptID; //部门id
        private static int DisplayType;
        private static string OffDutyTime = ""; //下班时间
        private static string OnDutyTime = "";

        private void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtDepartmentName.Text = "";
                OnDutyTime = "";
                OffDutyTime = "";

                SqlDataReader dr;

                //得到部门ID
                DeptID = (Request.QueryString["DeptID"] == null ? 0 : Int32.Parse(Request.QueryString["DeptID"]));

                var dp = new Components.Department();

                dr = dp.GetDepartmentInfo(DeptID);

                if (dr.Read())
                {
                    DepartmentName = dr["Department_Name"].ToString();
                    DepartmentDescription = dr["Department_Description"].ToString();
                }

                dr.Close();

                dr = dp.GetDepartmentDutyTime(DeptID);
                if (dr.Read())
                {
                    OnDutyTime = DateTime.Parse(dr["OnDutyTime"].ToString()).ToShortTimeString();
                    OffDutyTime = DateTime.Parse(dr["OffDutyTime"].ToString()).ToShortTimeString();
                }

                if (Request.QueryString["DisplayType"] != null)
                    DisplayType = Int32.Parse(Request.QueryString["DisplayType"]);
                else
                    DisplayType = 0;
                // 显示不同表格
                switch (DisplayType)
                {
                    case 0:
                        tabAdd.Visible = true;
                        tabDelete.Visible = false;
                        tabModify.Visible = false;
                        tabMove.Visible = false;

                        lbAddDepartment.BackColor = Color.FromArgb(0xf0f0f0);
                        lbDeleteDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbModifyDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbMoveDepartment.BackColor = Color.FromArgb(0xffffff);

                        addDepartmentName.Text = DepartmentName;
                        break;
                    case 1:
                        tabAdd.Visible = false;
                        tabDelete.Visible = true;
                        tabModify.Visible = false;
                        tabMove.Visible = false;
                        lbAddDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbDeleteDepartment.BackColor = Color.FromArgb(0xf0f0f0);
                        lbModifyDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbMoveDepartment.BackColor = Color.FromArgb(0xffffff);

                        delDepartmentName.Text = DepartmentName;
                        break;
                    case 2:
                        tabAdd.Visible = false;
                        tabDelete.Visible = false;
                        tabModify.Visible = true;
                        tabMove.Visible = false;

                        txtDepartmentName.Text = DepartmentName;
                        txtMDepartmentRemark.Text = DepartmentDescription;

                        lbAddDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbDeleteDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbModifyDepartment.BackColor = Color.FromArgb(0xf0f0f0);
                        lbMoveDepartment.BackColor = Color.FromArgb(0xffffff);

                        txtuOnDutyTime.Text = OnDutyTime;
                        txtuOffDutyTime.Text = OffDutyTime;
                        break;
                    case 3:
                        tabAdd.Visible = false;
                        tabDelete.Visible = false;
                        tabModify.Visible = false;
                        tabMove.Visible = true;

                        lbAddDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbDeleteDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbModifyDepartment.BackColor = Color.FromArgb(0xffffff);
                        lbMoveDepartment.BackColor = Color.FromArgb(0xf0f0f0);

                        lblDepartment.Text = DepartmentName;
                        BindDepartment();
                        break;
                    default:
                        break;
                }
            }
        }

        private void BindDepartment()
        {
            var db = new Database();
            using (SqlDataReader dr_department = db.RunDataReader("dbo.sproc_GetAllDepartment"))
            {
                lstDeparment.DataSource = dr_department;
                lstDeparment.DataTextField = "Department_Name";
                lstDeparment.DataValueField = "Department_ID";
                lstDeparment.DataBind();
            }
        }

        private void cmdAdd_ServerClick(object sender, EventArgs e)
        {
            if (txtADepartmentName.Text.Trim() != "")
            {
                long NewDeptID = Components.Department.Add(DeptID, txtADepartmentName.Text, txtADepartmentRemark.Text);
                if (DeptID > 0)
                {
                    if ((txtOnDutyTime.Text.Trim() != "") && (txtOffDutyTime.Text.Trim() != ""))
                    {
                        if (Components.Department.AddDutyTime(NewDeptID, DateTime.Parse(txtOnDutyTime.Text), DateTime.Parse(txtOffDutyTime.Text)) != 0)
                            Response.Write("<script laguage='javascript'>alert('添加部门出错！');</script>");
                        else
                            Response.Redirect("ListView.aspx?DeptID=" + DeptID + "&Refresh=1");
                    }
                    else
                        Response.Redirect("ListView.aspx?DeptID=" + DeptID + "&Refresh=1");
                }
                else
                    Response.Write("<script laguage='javascript'>alert('添加部门出错！');</script>");
            }
            else
            {
                litAMessage.Text = "请输入部门";
            }
        }

        private void cmdDelete_ServerClick(object sender, EventArgs e)
        {
            switch (Components.Department.Delete(DeptID))
            {
                case 0:
                    Response.Redirect("ListView.aspx?DeptID=" + DeptID + "&Refresh=1");
                    break;
                case -1:
                    Response.Write("<script laguage='javascript'>alert('有下级子部门，不能删除！');</script>");
                    break;
                case -2:
                    Response.Write("<script laguage='javascript'>alert('部门有成员，不能删除！');</script>");
                    break;
                default:
                    break;
            }
        }

        private void cmdModify_ServerClick(object sender, EventArgs e)
        {
            if (txtDepartmentName.Text.Trim() != "")
            {
                if ((Components.Department.Modify(DeptID, txtDepartmentName.Text, txtMDepartmentRemark.Text) == 0))
                {
                    if ((txtuOnDutyTime.Text.Trim() != "") && (txtuOffDutyTime.Text.Trim() != ""))
                    {
                        if ((Components.Department.UpdateDutyTime(DeptID, DateTime.Parse(txtuOnDutyTime.Text), DateTime.Parse(txtuOffDutyTime.Text)) != 0))
                        {
                            Response.Write("<script laguage='javascript'>alert('修改部门出错！');</script>");
                            return;
                        }
                    }
                    else
                    {
                        Components.Department.DeleteDutyTime(DeptID);
                    }
                    Response.Redirect("ListView.aspx?DeptID=" + DeptID + "&Refresh=1");
                }
                else
                    Response.Write("<script laguage='javascript'>alert('修改部门出错！');</script>");
            }
            else
                litMMessage.Text = "请输入部门";
        }

        private void lbAddDepartment_Click(object sender, EventArgs e)
        {
            Server.Transfer("ODepartment.aspx?DeptID=" + DeptID + "&DisplayType=0");
        }

        private void lbDeleteDepartment_Click(object sender, EventArgs e)
        {
            Server.Transfer("ODepartment.aspx?DeptID=" + DeptID + "&DisplayType=1");
        }

        private void lbModifyDepartment_Click(object sender, EventArgs e)
        {
            Server.Transfer("ODepartment.aspx?DeptID=" + DeptID + "&DisplayType=2");
        }

        private void lbMoveDepartment_Click(object sender, EventArgs e)
        {
            Server.Transfer("ODepartment.aspx?DeptID=" + DeptID + "&DisplayType=3");
        }

        private void cmdMove_Click(object sender, EventArgs e)
        {
            var dept = new Components.Department();

            switch (dept.MoveDeparement(DeptID, Int32.Parse(lstDeparment.Items[lstDeparment.SelectedIndex].Value)))
            {
                case 0:
                    Response.Write("<script laguage='javascript'>alert('移动到" + lstDeparment.Items[lstDeparment.SelectedIndex].Text + " 部门成功！');</script>");
                    Response.Redirect("ListView.aspx?DeptID=" + DeptID + "&Refresh=1");
                    break;
                case -1:
                    Response.Write("<script laguage='javascript'>alert('不能移动到下级部门！');</script>");
                    break;
                default:
                    break;
            }
        }

        private void cmdReturn_Click(object sender, EventArgs e)
        {
            Server.Transfer("Listview.aspx?DeptID=" + DeptID);
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lbAddDepartment.Click += new System.EventHandler(this.lbAddDepartment_Click);
            this.lbDeleteDepartment.Click += new System.EventHandler(this.lbDeleteDepartment_Click);
            this.lbModifyDepartment.Click += new System.EventHandler(this.lbModifyDepartment_Click);
            this.lbMoveDepartment.Click += new System.EventHandler(this.lbMoveDepartment_Click);
            this.cmdAdd.ServerClick += new System.EventHandler(this.cmdAdd_ServerClick);
            this.cmdAddReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdDeleteReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdModifyReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdMove.Click += new System.EventHandler(this.cmdMove_Click);
            this.cmdMoveReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdDelete.ServerClick += new System.EventHandler(this.cmdDelete_ServerClick);
            this.cmdModify.ServerClick += new System.EventHandler(this.cmdModify_ServerClick);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}